<template>
  <div class="category">
    <u-sticky bgColor="#fff">
      <view class="pubuBox">
        <u-search placeholder="请输入关键字"  v-model="query" @search="getProductList" @custom="getProductList"></u-search>
      </view>
      <u-line></u-line>
      <u-tabs :current="current" :list="categoryList" @change="handleClickCategory"></u-tabs>
    </u-sticky>
    <view class="wp-relative grid">
      <view class="product-item" v-for="item in productList" @click="productDetail(item)">
        <u-image  :src="item.post_url" style="width: 100%;height: 228rpx;
      border-radius: 12rpx;
      border-bottom-left-radius: 0;
      border-bottom-right-radius: 0;
      overflow: hidden;"></u-image>
        <div style="padding: 0 24rpx">
          <view class="product-name">{{ item.name }}</view>
          <view style="display: flex;align-items: center;justify-content: space-between">
            <view class="product-count">5000人次学习</view>
            <view class="product-status">公开</view>
          </view>
        </div>
      </view>
    </view>

    <u-empty v-if="productList.length === 0" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      query: '',
      current: 0,
      categoryList: [],
      activeId: '',
      productList: []
    }
  },
  onShow() {
    this.getCategoryList()
  },
  methods: {
    getCategoryList() {
      this.$api.categoryList().then(res => {
        this.categoryList = res
        if (res.length > 0) {
          this.activeId = res[0].id
          this.getProductList()
        }
      })
    },
    handleClickCategory(item) {
      this.current = item;
      this.activeId = this.categoryList[item].id
      this.getProductList()
    },
    getProductList() {
      uni.showLoading({
        title: '加载中',
        mask: true,
        duration: 0
      })
      this.$api.getCourseList({ category_id: this.activeId }).then(res => {
        this.productList = res
      }).finally(() => {
        uni.hideLoading()
      })
    },
    productDetail(item) {
      uni.navigateTo({
        url: `/pages/course/detail?id=${item.id}`
      })
    },
  }
}
</script>

<style lang="scss">
.category {
  height: 100%;
  overflow: auto;
  padding-bottom: 60rpx;
  .pubuBox {
    padding: 20rpx;
  }
  .wp-relative {
    display: flex;
    flex-wrap: wrap;
    margin-top: 24rpx;
    padding: 0 12rpx;
    .product-item {
      width: calc(50% - 24rpx);
      background-color: #ffffff;
      margin: 12rpx 12rpx;
      padding-bottom: 24rpx;
      .product-name {
        width: 100%;
        font-size: 28rpx;
        line-height: 28rpx;
        color: #333333;
        margin-top: 24rpx;
      }

      .product-count {
        margin-top: 24rpx;
        font-size: 22rpx;
        font-family: 萍方-简;
        line-height: 20rpx;
        color: #999999;
      }
      .product-status {
        margin-top: 24rpx;
        font-size: 26rpx;
        font-family: 萍方-简;
        line-height: 26rpx;
        color: #fe9b1a;
      }
    }
  }
  .p-list {
    flex-grow: 1;
    padding: 0 10rpx;
    background-color: #fff;
    .p-item {
      padding: 20rpx;
      box-sizing: border-box;
      display: flex;
      border-bottom: 1px solid #f0f0f0;
      img {
        width: 80px;
        height: 80px;
      }
      .p-desc {
        flex: 1;
        padding-left: 20rpx;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .p-title {
          font-size: 30rpx;
          color: #606266;
          margin-bottom: 10rpx;
        }
        .p-price-box {
          display: flex;
          justify-content: space-between;
          align-items: center;
          .add-num{
            padding: 0 16rpx;
          }
          .p-price {
            font-size: 30rpx;
            color: #333;
          }
          .add-icon {
            width: 40rpx;
            height: 40rpx;
            line-height: 40rpx;
            text-align: center;
            background-color: rgb(61, 155, 246);
            color: #fff;
            border-radius: 50%;
          }
        }
      }
    }
  }
  .category-content {
    display: flex;
    height: 100%;
    overflow: hidden;
  }
  .c-silde {
    background-color: #f8f8f8;
    width: 160rpx;
  }
  .c-item {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 52px;
    font-size: 14px;
    color: #606266;
    position: relative;
    &::before {
      content: "";
      position: absolute;
      left: 0;
      top: 50%;
      -webkit-transform: translateY(-50%);
      transform: translateY(-50%);
      height: 15px;
      width: 4px;
      opacity: .8;
      background-color: transparent;
    }
    &.active {
      color: rgb(61, 155, 246);
          background-color: #fff;
        &::before {
          background-color: rgb(61, 155, 246);
        }
     }
  }
  .top {
    background-repeat: no-repeat;
    background-size: cover;
    width: 100%;
    height: 125rpx;
    position: fixed;
    top: 0;
    z-index: 9;
    background-image: url("@/static/top.png");
    .top-title {
      position:absolute;
      top: 30rpx;
      width: 100%;
      text-align: center;
      font-size: 36rpx;
      color: #FFFFFF;
      font-weight: bold;
    }
  }
}
</style>
